//选择楼栋组件
import 'dart:math';

import 'package:flutter/material.dart';

class BuildingList extends StatefulWidget {
  BuildingList({super.key, required this.point});
  //定义社区名称,传入进来
  String point;

  @override
  State<BuildingList> createState() => _BuildingListState();
}

class _BuildingListState extends State<BuildingList> {
  Map houseData = {
    'point': '', //社区名称
    'size': 0, //楼栋数量(随机数生成)
    'type': '', //楼栋类型(单元 or 栋)
  };

  @override
  void initState() {
    super.initState();
    setState(() {
      houseData['point'] = widget.point;
    });
    fakeData();
  }

  //生成随机的房子数量
  //需求
  //1.随机生成3-8个
  //2.如果是小于5个的,则展示单元,如果是大于5个的,则展示栋
  fakeData() {
    //随机数对象
    Random random = Random();
    int size = random.nextInt(5) + 3;
    String type = size < 5 ? '单元' : '栋';
    setState(() {
      houseData['size'] = size;
      houseData['type'] = type;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 设置导航条
      appBar: AppBar(
        title: Text('选择楼栋'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      // 设置主体内容
      body: ListView.separated(
        padding: EdgeInsets.all(10),
        itemBuilder: (ctx, index) {
          return GestureDetector(
            onTap: () {
              Navigator.pushNamed(
                context,
                '/room_list',
                arguments: {
                  'point': houseData['point'],
                  'building': '${index + 1}${houseData['type']}',
                },
              );
            },
            child: Row(
              children: [
                //左侧文字
                Expanded(
                  child: Text(
                    '${houseData['point']}${index + 1}${houseData['type']}',
                  ),
                ),
                //右侧图标
                Icon(Icons.arrow_forward_ios, size: 20),
              ],
            ),
          );
        },
        separatorBuilder: (ctx, index) {
          //分割线
          return SizedBox(height: 20);
        },
        itemCount: houseData['size'],
      ),
    );
  }
}
